package com.randb.easyhttp.interceptor;

import com.randb.easyhttp.model.HttpHeaders;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: RandBII
 * Date: 2020/5/15
 * Description:
 */
public class HeadersInterceptor implements Interceptor {

    private HttpHeaders headers;

    public HeadersInterceptor(HttpHeaders headers) {
        this.headers = headers;
    }


    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        if (headers.headersMap.isEmpty()) return chain.proceed(builder.build());

        for (Map.Entry<String, String> entry : headers.headersMap.entrySet()) {
            builder.header(entry.getKey(), entry.getValue()).build();
        }
        return chain.proceed(builder.build());
    }


}
